package com.jt.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.exception.ServiceException;
import com.jt.mapper.CartMapper;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.service.DubboCartService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 购物车服务生产者
 * @author dc
 * @Classname CartServiceImpl
 * @Date 2020/2/28 14:45
 */
@Service
public class CartServiceImpl implements DubboCartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<Cart> findCartListByUserId(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public void saveCart(Cart cart) {
        //查询数据库购物车记录
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id",cart.getItemId());
        queryWrapper.eq("user_id",cart.getUserId());
        Cart cartDB = cartMapper.selectOne(queryWrapper);
        if(cartDB==null){
            //用户第一次购买,直接新增数据
            cart.setCreated(new Date()).setUpdated(cart.getCreated());
            cartMapper.insert(cart);
        }else{
            int num = cartDB.getNum()+cart.getNum();
            Cart cartTemp=new Cart();
            //利用一个中间对象,更新只需要更新的字段
            cartTemp.setNum(num).setId(cartDB.getId()).setUpdated(new Date());
            cartMapper.updateById(cartTemp);
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void updateItemNum(Cart cart) {
        Cart tempCart = new Cart();
        tempCart.setNum(cart.getNum()).setUpdated(new Date());
        UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<Cart>();
        updateWrapper.eq("user_id", cart.getUserId()).eq("item_id", cart.getItemId());
        int rows = cartMapper.update(tempCart, updateWrapper);
        if (rows<1){
            SysResult.fail();
        }
    }
    /**
     * 将对象中不为null的属性当做where条件
     * 前提:保证cart中只能有2个属性不为null.
     */
    @Override
    public void deleteCart(Cart cart) {
        QueryWrapper<Cart> queryWrapper =
                new QueryWrapper<Cart>(cart);
        cartMapper.delete(queryWrapper);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void deleteCartAfterPay(Order order) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        Long userId = order.getUserId();
        List<OrderItem> orderItems = order.getOrderItems();
        for(OrderItem item : orderItems){
            queryWrapper.eq("item_id",item.getItemId()).eq("user_id",userId);
            int rows = cartMapper.delete(queryWrapper);
            if(rows<1){
                throw new ServiceException("网络出现故障，自动清除购物车失败");
            }
        }
    }
}
